﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repository;
using SportsStore.Model;
using SportsStore.Repository.Contexts;
using SportsStore.Repository.Interfaces;

namespace SportsStore.Repository.Repositories
{
  public class CartItemRepository : BaseEfRepository<CartItem>, ICartItemRepository
  {
    public CartItemRepository(string connectionString)
          : base(new SportsStoreDbContext(connectionString))
    {
    }

    public CartItem GetCart(int productId, string shoppingCartId)
    {
      return (from c in Entities
              where c.ProductId == productId
                    && c.CartId == shoppingCartId
              select c).FirstOrDefault();
    }

    public List<CartItem> GetCart(string shoppingCartId)
    {
      return Entities.Where(cart => cart.CartId == shoppingCartId).ToList();
    }

    public decimal GetTotal(string shoppingCartId)
    {
      decimal? total = (from c in Entities
                        where c.CartId == shoppingCartId
                        select (int?) c.Quantity * c.Product.Price).Sum();
      return total ?? decimal.Zero;
    }
  }
}
